home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-02-19 | 5.6 KB | 180 lines | [TEXT/CWIE] |
-
-
- #include "HFSBackingStore.h"
- #include "MoreStrings.h"
- #include "Exceptions.h"
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::THFSBackingStore
- //--------------------------------------------------------------------------------
- THFSBackingStore::THFSBackingStore()
- {
- fRefNum = TOpenFileRefNum();
- fFileEOF = 0;
- // ••• set up an arbitrary FSSpec here in the temporary items folder
- this->OpenFile();
- } // THFSBackingStore::THFSBackingStore
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::THFSBackingStore
- //--------------------------------------------------------------------------------
- THFSBackingStore::THFSBackingStore(TFSSpecification& fileSpec)
- {
- fRefNum = TOpenFileRefNum();
- fFileEOF = 0;
- fFileSpec = fileSpec;
-
- //
- // Fill in from fileSpec?
- //
- fFileCreator = 0;
- fFileType = 0;
-
- this->OpenFile();
- } // THFSBackingStore::THFSBackingStore
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::THFSBackingStore
- //--------------------------------------------------------------------------------
- THFSBackingStore::THFSBackingStore(TFSSpecification& fileSpec, OSType fileCreator, OSType fileType)
- {
- fRefNum = TOpenFileRefNum();
- fFileEOF = 0;
- fFileSpec = fileSpec;
- fFileCreator = fileCreator;
- fFileType = fileType;
-
- this->OpenFile();
- } // THFSBackingStore::THFSBackingStore
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::~THFSBackingStore
- //--------------------------------------------------------------------------------
- THFSBackingStore::~THFSBackingStore()
- {
- this->CloseFile();
- } // THFSBackingStore::~THFSBackingStore
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::BackingStoreType
- //--------------------------------------------------------------------------------
- long THFSBackingStore::BackingStoreType() const
- {
- return kHFSBackingStore;
- }
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::CanSaveDocument
- //--------------------------------------------------------------------------------
- Boolean THFSBackingStore::CanSaveDocument()
- {
- //
- // •Should also test to see if the file or volume is locked.
- //
- return TAbstractBackingStore::CanSaveDocument() && (fRefNum.IsOpen());
- }
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::Read
- //--------------------------------------------------------------------------------
- void THFSBackingStore::Read(void* bufferStart, Int64 startByteInFile, long numberOfBytes)
- {
- //
- // Fail if the file is not open
- //
- if(fRefNum.IsOpen())
- {
- long bytesToRead = numberOfBytes;
- FailErr(fRefNum.SetFilePosition(startByteInFile));
- FailErr(fRefNum.Read(&bytesToRead, bufferStart));
- ASSERT(bytesToRead == numberOfBytes);
- }
- else
- FailErr(fnOpnErr);
- } // THFSBackingStore::Read
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::Write
- //--------------------------------------------------------------------------------
- void THFSBackingStore::Write(void* bufferStart, Int64 startByteInFile, long numberOfBytes)
- {
- //
- // Fail if the file is not open.
- //
- // CanSaveDocument is (should be) more restrictive than IsOpen
- // because it checks (should check) things like locked volumes
- //
- if(this->CanSaveDocument())
- {
- long bytesToWrite = numberOfBytes;
-
- if(startByteInFile + numberOfBytes > fFileEOF)
- {
- Int64 newEOF = startByteInFile + numberOfBytes;
- FailErr(fRefNum.SetEndOfFile(newEOF));
- fFileEOF = newEOF;
- }
-
- FailErr(fRefNum.SetFilePosition(startByteInFile));
- FailErr(fRefNum.Write(&bytesToWrite, bufferStart));
- ASSERT(bytesToWrite == numberOfBytes);
- }
- //
- // If 'CanSaveDocument' returned false because the volume
- // was locked, then this would give us the wrong error. Hm.
- //
- else
- FailErr(fnOpnErr);
- } // THFSBackingStore::Write
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::OpenFile
- //--------------------------------------------------------------------------------
- void THFSBackingStore::OpenFile()
- {
- if(fRefNum.IsOpen() == false)
- {
- //
- // Should create the file if it doesn't exist
- // If the file does exist, then we should require
- // that its file type is what we expect it to be.
- //
-
- //
- // Currently we always open a file read/write. It
- // might be nice to allow read-only access to a file
- // as well, but currently there's no way to specify that.
- //
- FailErr(fRefNum.Open(fFileSpec));
-
- OffsetInFile theEndOfFile;
- FailErr(fRefNum.GetEndOfFile(&theEndOfFile));
- fFileEOF = theEndOfFile;
- }
- } // THFSBackingStore::OpenFile
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::CloseFile
- //--------------------------------------------------------------------------------
- void THFSBackingStore::CloseFile()
- {
- //
- // We never want 'Close' to fail.
- //
- fRefNum.Close();
- #if 0 //◊
- FlushVol("\p", fFileSpec.VRefNum()); // ◊◊◊ should be a method of fFileSpec, perhaps?
- #endif
- } // THFSBackingStore::CloseFile
-
- //--------------------------------------------------------------------------------
- // THFSBackingStore::DocumentName
- //--------------------------------------------------------------------------------
- void THFSBackingStore::DocumentName(TUpdataDataReference& name)
- {
- TString specificationName;
- fFileSpec.Name(specificationName);
-
- name.CopyFrom(TConstDataReference(typeChar, (char*)specificationName.TextStart(), specificationName.StringLength()));
- }
-